#include "V8ScriptPlugin.h"
#include <OgreRoot.h>

V8ScriptPlugin* plugin;

extern "C" void SCRIPT_PLUGIN_EXPORT dllStartPlugin () {
    plugin = OGRE_NEW V8ScriptPlugin();
    Root::getSingleton().installPlugin (plugin);
}

extern "C" void SCRIPT_PLUGIN_EXPORT dllStopPlugin () {
    Root::getSingleton().uninstallPlugin (plugin);
    OGRE_DELETE plugin;
}




// really should do some proper argument checking on these functions...

Handle<Value> AddLight(const Arguments& args)
{
    HandleScope handle_scope;

    v8::String::Utf8Value str(args[0]);
    Ogre::String lightName(ToCString(str));

    Light* light = plugin->sceneManager->createLight(lightName);
    light->setDiffuseColour(Real(args[1]->NumberValue()), Real(args[2]->NumberValue()), Real(args[3]->NumberValue()));
    light->setPosition(Real(args[4]->NumberValue()), Real(args[5]->NumberValue()), Real(args[6]->NumberValue()));

    return v8::Undefined();
}

Handle<Value> AddEntity(const Arguments& args)
{
    HandleScope handle_scope;

    v8::String::Utf8Value s1(args[0]);
    Ogre::String entityName(ToCString(s1));

    v8::String::Utf8Value s2(args[1]);
    Ogre::String meshName(ToCString(s2));
    
    Entity* entity = plugin->sceneManager->createEntity(entityName, meshName);

    SceneNode* node = plugin->sceneManager->getRootSceneNode()->createChildSceneNode();
    node->attachObject((MovableObject*)entity);
    node->setPosition(Real(args[2]->NumberValue()), Real(args[3]->NumberValue()), Real(args[4]->NumberValue()));

    return v8::Undefined();

}